# coding:utf8

from appserver import db_store
from models.entity.device_with_sensor import DeviceWithSensor
from models.entity.model_base import ModelBase
import uuid

"""
传感器表
"""


class Sensor(ModelBase):
    __tablename__ = "sensors"
    __bind_key__ = "main"

    # autoincrement=True,
    id = db_store.Column(db_store.Integer, primary_key=True, comment=u'主键')
    code = db_store.Column(db_store.String(64), nullable=False, unique=True, comment=u"GUID")
    name = db_store.Column(db_store.String(64), nullable=False, unique=True, comment=u'传感器名称')
    description = db_store.Column(db_store.String(255), nullable=False, comment=u"描述")
    sensor_type_id = db_store.Column(db_store.Integer, db_store.ForeignKey('sensor_types.id'), nullable=False,
                                     comment=u"传感器分类ID")
    sensor_group_id = db_store.Column(db_store.Integer, db_store.ForeignKey("sensor_groups.id"),comment=u"传感器分组id")
    icon = db_store.Column(db_store.String(32), comment="图标")
    dis_order = db_store.Column(db_store.Integer, comment=u"排序",default=0)
    # 以下的字段是新加的
    eng_unit = db_store.Column(db_store.String(10), comment=u"工程单位")
    data_type = db_store.Column(db_store.String(32),comment=u"数据类型")
    # data_type的值:
    #       WORD             INT16
    #       DWORD          INT32
    #       FLOAT             浮点数
    #       DOUBLE         双精度浮点数
    #       STRING           字符串
    #       LONG               长整数
    #       LOCATION      经纬度
    
    # 新加的字段结束，以下的字段是需要修改的
    # 预警上下限
    h = db_store.Column(db_store.Float,  comment=u"预警上限")
    l = db_store.Column(db_store.Float, comment=u"预警下限")
    # 报警上下限
    hh = db_store.Column(db_store.Float,  comment=u"报警上限")
    ll = db_store.Column(db_store.Float, comment=u"报警下限")
    # 工程上下限
    range_max  = db_store.Column(db_store.Float,  comment=u"工程上限")
    range_min  = db_store.Column(db_store.Float, comment=u"工程下限")
    # 远程服务器相关
    topic = db_store.Column(db_store.String(255), comment=u"MQTT topic")
    client_id = db_store.Column(db_store.String(64),comment=u"MQTT client id")
    remote_host_id = db_store.Column(db_store.Integer,db_store.ForeignKey("remote_hosts.id"),comment=u"远程服务器信息")
    # 说明结束
    status = db_store.Column(db_store.Integer, comment=u"状态", default=0)  # 0-正常 1-禁用 2-弃用
    # 传感器值表达式
    is_exp = db_store.Column(db_store.Integer,comment=u"是否存在表达式",default=0) # 0-无 1-有
    exp = db_store.Column(db_store.String(255),comment=u"表达式")

    def __repr__(self):
        return "<Sensor %s>" % self.name

    def save(self):
        try:
            if self.id is None:
                self.id = self.get_max_id() + 1
            if self.code is None:
                self.code = uuid.uuid4().hex
            db_store.session.add(self)
            db_store.session.commit()
            return True
        except Exception as e:
            print(e)
            db_store.session.rollback()
            return False

    @staticmethod
    def get_sensor_by_name(name):
        return Sensor.query.filter_by(name=name).first()

    @staticmethod
    def get_sensor_by_code(code):
        return Sensor.query.filter_by(code=code).first()

    @staticmethod
    def get_sensors_by_device_id(device_id):
        rlist=[]
        connectors=DeviceWithSensor.query.filter_by(device_id=device_id).all()
        for item in connectors:
            rlist.append(Sensor.query.filter_by(id=item.sensor_id).first())
        return rlist
